package com.example.module_car.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.module_car.R
import com.example.module_car.adapter.CarPaymentsAdapter
import com.example.module_car.adapter.CarProvincesAdapter
import com.example.module_car.databinding.ActivityAddCarBinding
import com.example.module_car.vm.CarViewModel
import com.yuwei.base.BaseActivity
import com.yuwei.common.path.RouterPath
import com.yuwei.http.entity.VehiclesBody
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.ADD_CAR)
class AddCarActivity : BaseActivity<ActivityAddCarBinding,CarViewModel>() {
    val carProvincesAdapter = CarProvincesAdapter()
    var mPosition = -1
    override fun initView() {
        binding.ivCarPeriod.setOnClickListener {
            ARouter.getInstance().build(RouterPath.CAR_DISTINGUISH).navigation()
        }
        binding.rvVehicleAddCars.adapter = carProvincesAdapter
        binding.rvVehicleAddCars.layoutManager = GridLayoutManager(this,7)
        vm.getProvinces()
        carProvincesAdapter.setOnItemClickListener { adapter, view, position ->
            mPosition = position
            carProvincesAdapter.setPosition(mPosition)
            binding.btnCarNumber.text = carProvincesAdapter.data[mPosition].abbreviation
            carProvincesAdapter.notifyDataSetChanged()
        }
        binding.back.setOnClickListener {
            finish()
        }
        binding.tvAddCar.setOnClickListener {
            if (binding.etCarNumber.text.toString().isEmpty()){
                ToastUtils.showShort("请输入车牌号")
                binding.etCarNumber.error = "请输入车牌号"
                return@setOnClickListener
            }
            val vehiclesBody = VehiclesBody(
                id = 0,
                licensePlate = binding.btnCarNumber.text.toString()+binding.etCarNumber.text.toString(),
                ownerName = "张三",
                provinceAbbreviation = binding.btnCarNumber.text.toString(),
                userId = 1
            )
            vm.getAddCar(vehiclesBody)
        }

    }

    override fun initData() {
        vm.getCarProvincesSuccess.observe(this){
            carProvincesAdapter.data.addAll(it)
            carProvincesAdapter.notifyDataSetChanged()
        }
        vm.getAddCarSuccess.observe(this){
            ToastUtils.showShort("添加成功")
            finish()
            ARouter.getInstance().build(RouterPath.ADD_PAYMENTS)
                .withString("num",it.licensePlate)
                .withString("name",it.ownerName).navigation()
        }
    }

}